package jflowmap.model.ontology;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public final class Match {

	private final Set<Dimension> dimensions = new HashSet<Dimension>();
	
	public Match (Collection<Dimension> dimensions) {
		this.dimensions.addAll(dimensions);
	}
	
	public Match (Dimension... dimensions) {
		Collections.addAll(this.dimensions, dimensions);
	}
	
	public Match add (Dimension... dimensions) {
		Collections.addAll(this.dimensions, dimensions);
		return this;
	}
	
	public Match add (Collection<Dimension> dimensions) {
		this.dimensions.addAll(dimensions);
		return this;
	}
	
	public boolean matches (final Value value) {
		final Set<Dimension> valueDims = value.getDimensions();
		
		for (Dimension d: dimensions) {
			if (!valueDims.contains(d))
				return false;
		}
		
		return true;
	}
	
	public Set<Dimension> getDimensions() {
		return Collections.unmodifiableSet(dimensions);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((dimensions == null) ? 0 : dimensions.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Match other = (Match) obj;
		if (dimensions == null) {
			if (other.dimensions != null)
				return false;
		} else if (!dimensions.equals(other.dimensions))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Match [dimensions=" + dimensions + "]";
	}
}
